package com.clz.hyperit.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.clz.hyperit.dao.RoleDao;
import com.clz.hyperit.entity.Role;
import com.clz.hyperit.exception.ApplicationRunTimeException;
import com.clz.hyperit.service.RoleService;
@Service("roleService")
public class RoleServiceImpl implements RoleService {
	
	private static Logger logger = LoggerFactory
			.getLogger(RoleServiceImpl.class);
	@Autowired
	private RoleDao roleDao;

	public boolean addRole(Role role) {
		int flag;
		if(roleDao.getByName(role.getName())==null){
			flag = roleDao.save(role);
			if(flag<=0){
				throw new ApplicationRunTimeException("添加角色失败");
			}
		}
		else{
			throw new ApplicationRunTimeException("该角色已存在");
		}
		return true;
		
	}

	public void deleteRole(Integer roleId) {
		// TODO Auto-generated method stub
		Role role = roleDao.get(roleId);
		if(role!=null){
			roleDao.delete(role);
		}
	}
	public void updateRole(Role role){
		if(role.getId()!=null){
			roleDao.updateWithNotNullProperties(role);
		}
		else{
			throw new ApplicationRunTimeException("更新角色信息失败");
		}
	}
	public List<Role> findRoles() {
		return roleDao.findAll();
	}

	public Role findRole(Role role) {
		// TODO Auto-generated method stub
		return null;
	}

}
